import apple from "../main";
import {removeSessionStore} from "./util";

/**
 * 校验手机号
 * @param phone
 * @returns {boolean}
 */
export function checkPhone(phone) {
  let reg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
  if (phone == "" || phone == undefined || phone == null) {
    apple.$message({message: "手机号码不能为空", center: true, type: 'error', duration: 1000});
    return false;
  } else if (!reg.test(phone)) {
    apple.$message({message: "手机号码格式错误", center: true, type: 'error', duration: 1000});
    return false;
  }
  return true;
}


/**
 * 校验密码
 * @param pwd
 * @returns {boolean}
 */
export function checkPwd(pwd, type) {
  if (pwd == "" || pwd == undefined || pwd == null) {
    apple.$message({message: "密码不能为空", center: true, type: 'error', duration: 1000});
    return false;
  } else if (pwd.indexOf(" ") >= 0) {
    apple.$message({message: "密码中不能有空格字符", center: true, type: 'error', duration: 1000});
    return false;
  } else if ((/[\u4e00-\u9fa5]+/).test(pwd)) {
    apple.$message({message: "密码不能包含中文", center: true, type: 'error', duration: 1000});
    return false;
  }
  if (type == 2) {
    if (pwd.length < 6 || pwd.length > 16) {
      apple.$message({message: "密码长度为6-16位", center: true, type: 'error', duration: 1000});
      return false;
    }
  }
  return true;
}

/**
 * 校验两次密码一致
 * @param pwd1
 * @param pwd2
 * @returns {boolean}
 */
export function affirmPwd(pwd1, pwd2) {
  if (pwd1 != pwd2) {
    apple.$message({message: "两次密码不一致", center: true, type: 'error', duration: 1000});
    return false;
  }
  return true;
}

/**
 * 校验验证码
 * @returns {boolean}
 */
export function checkCode(code) {
  let reg = /^[0-9]*$/;
  if (code == "" || code == undefined || code == null) {
    apple.$message({message: "验证码不能为空", center: true, type: 'error', duration: 1000});
    return false;
  } else if (!reg.test(code)) {
    apple.$message({message: "验证码格式不正确", center: true, type: 'error', duration: 1000});
    return false;
  }
  return true;
}

/**
 * 校验姓名
 * @param name
 * @returns {boolean}
 */
export function checkName(name) {
  let reg = /^[\u2E80-\u9FFF]+$/;
  if (name == undefined || name == "") {
    apple.$message({message: "请输入您的姓名", center: true, type: 'error', duration: 1000});
    return false;
  } else if (!reg.test(name)) {
    apple.$message({message: "请输入您的中文姓名", center: true, type: 'error', duration: 1000});
    return false;
  } else if (name.length > 5) {
    apple.$message({message: "姓名最多5位", center: true, type: 'error', duration: 1000});
    return false;
  }
  return true;
}

/**
 * 清除缓存
 */
export function clearCache() {
  removeSessionStore("searchKeyWord");
  removeSessionStore("searchCity");
  removeSessionStore("searchTown");
  removeSessionStore("searchFactory");
  removeSessionStore("searchWorkType");
}

